/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package familytree.struts.actions;

import familytree.negocio.IArbolProcessor;
import familytree.negocio.INodoProcessor;
import familytree.negocio.IUsuarioProcessor;
import familytree.negocio.bo.ArbolBO;
import familytree.negocio.bo.ArbolConstant;
import familytree.negocio.bo.EventoBO;
import familytree.negocio.IEventoProcessor;
import familytree.negocio.bo.NodoBO;
import familytree.negocio.bo.UsuarioBO;
import familytree.persistencia.pojos.Evento;
import familytree.persistencia.pojos.Nodo;
import familytree.persistencia.pojos.Usuario;
import familytree.struts.forms.LoginForm;
import familytree.util.ITree;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author Antonio
 */
public class LoginAction extends Action {
    
    /* forward name="OK" path="" */
    private final static String OK = "ok";
    private final static String FALLO = "fallo";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        LoginForm loginForm = (LoginForm)form;
        String correo = loginForm.getCorreo();
        String password = loginForm.getPassword();
        IUsuarioProcessor usuarioBD = new UsuarioBO();
        Usuario usuario = usuarioBD.getUsuario(correo,password);
        if (usuario == null){
            
            Integer incorrecto = 1;
            request.setAttribute("incorrecto",incorrecto );
            return mapping.findForward(FALLO);
        }
        else{
            
            INodoProcessor nodoBD = new NodoBO();
            IArbolProcessor arbolProcessor = new ArbolBO();
            Nodo nodo = nodoBD.getNodo(usuario);
            ITree[] familia = arbolProcessor.createTrees(nodo);
            String[][] localizacion = {{ArbolConstant.TU},{nodo.getIdNodo()}};            
            request.getSession().setAttribute("familia", familia);
            request.getSession().setAttribute("nodoActual", nodo);
            request.getSession().setAttribute("usuarioActual", usuario);
            request.getSession().setAttribute("localizacion", localizacion);            
        }
            
        
        return mapping.findForward(OK);
        
    }
}
